package com.hmdp.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.hmdp.dto.UserDTO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class LoginInterceptor implements HandlerInterceptor {


    //进入controller之前执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //1.判断是否需要拦截(ThreadLocal中是否有用户信息)
        if(UserHolder.getUser() == null){
            response.setStatus(401);
            return false;
        }
        //有用户
        return true;

    }

    //controller执行之后执行,不管是否有异常
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //清除ThreadLocal中的用户信息
        UserHolder.removeUser();

        //必须做remove,内存泄露就是ThreadLocal占用jvm内存部分,没有被使用1,却一直占用叫内存泄露
    }
    //想让他生效需要配置拦截器配置类
}
